package com.ktjiaoyu.crm;

import com.ktjiaoyu.crm.entity.User;
import com.ktjiaoyu.crm.repository.RoleRepository;
import com.ktjiaoyu.crm.repository.UserRepository;
import com.ktjiaoyu.crm.service.UserService;
import com.ktjiaoyu.crm.vo.UserInfo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTester {
    @Resource
    private UserRepository userRepository;
    @Resource
    private RoleRepository roleRepository;
    @Resource
    private UserService userService;

    @Test
    public void testInsert() {//测试新增
        User user = new User(1, "JPA", "123456", 1L);
        userRepository.save(user);
    }

    public void testGet2() {//测试按主键查询
        User user = userRepository.findById(2L).get();
        System.out.println("usrName:" + user.getUsrName());

    }

    @Test
    public void testFindByUsrNameLike() {//测试按用户名模糊查询
        List<User> list = userRepository.findByUsrNameLike("%s%");//查询参数必须带%号
        if (list != null) {
            for (User user : list) {
                System.out.println("usrName:" + user.getUsrName());
            }
        }
    }

    @Test
    public void testFindPageByUsrRoleId() {//测试按角色查询用户并分页
        int page = 0, size = 2;//分页要素，页数从零开始
        //控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId");
        //控制分页的辅助类，可以设置页码，每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<User> userPage = userRepository.findPageByUsrRoleId(8L, pageable);

        System.out.println("总记录数：" + userPage.getTotalElements());
        System.out.println("总页数：" + userPage.getTotalPages());
        System.out.println("当前页数：" + (userPage.getNumber() + 1));
        System.out.println("每页记录数：" + userPage.getSize());
        System.out.println("当前页记录数：" + userPage.getNumberOfElements());
        for (User user : userPage.getContent()) {
            System.out.println("usrId：" + user.getUsrId());
        }
    }

    @Test
    public void testFindPageByMap() {
        int page = 0, size = 2;//分页要素
        //控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId");
        //控制分页的辅助类，可以设置页码，每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Map param = new HashMap();
        param.put("roleId", 8L);//测试查询条件，也可不给或给多个
        Page<User> userPage = userService.findPageByMap(param, pageable);
        System.out.println("总记录数--------->" + userPage.getTotalElements());
        System.out.println("总页数--------->" + userPage.getTotalPages());
        System.out.println("当前页数--------->" + (userPage.getNumber()));
        System.out.println("每页记录数--------->" + userPage.getSize());
        System.out.println("当前记录--------->");
        for (User user : userPage.getContent()) {//获得查询记录
            System.out.println("usrId:" + user.getUsrId());
        }
    }

    //    @Test
//    public void testSelect(){//测试查询
//        User user=userRepository.findById(2L).get();
//        System.out.println("id："+user.getUsrId() +"\tname："+ user.getUsrName() +"\troleId："+ user.getUsrRoleId() +
//                "\tusrFlag："+ user.getUsrFlag());
//    }
//    @Test
//    public void testInsertUser (){//测试新增
//        User user=new User(1,"JPA","123456",1L);
//        userRepository.save(user);
//    }
    @Test
    public void testUpdateInfo() {//测试修改
        userRepository.modifyNameById("JPA111", 12L);
    }

    @Test
    public void testDel() {//测试删除
        userRepository.deleteById(12L);

    }


    //第三章
    @Test
    public void testGetUserInfo() {//测试多表连接查询
        UserInfo userInfo = userRepository.getUserInfo(2L);
        System.out.println("usrName:" + userInfo.getUsrName());
        System.out.println("roleName:" + userInfo.getRoleName());

    }


}
